package com.ah.cookie.autologin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ah.util.Const;
@WebServlet("/ServletLoginAuto")
public class C1ServletLoginAuto extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uname = null;
		String pwd = null;
		// ----------------------------
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			System.out.println("没有Cookie（通常是第一次登陆该网站）");
			response.sendRedirect(Const.P_LOGIN);
			return;
		}
		for (Cookie cookie : cookies) {
			String ckName = cookie.getName();
			if ("uname".equals(ckName)) {
				uname = cookie.getValue();
			}
			if ("pwd".equals(ckName)) {
				pwd = cookie.getValue();
			}
		}
		if (C4ServiceLogin.login(uname, pwd) == true) {
			System.out.println("自动登录成功\r\n===");
			response.sendRedirect(Const.P_SUCCESS);
		} else {
			System.out.println("自动登录失败\r\n===");
			response.sendRedirect(Const.P_LOGIN);
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
}
